//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using Microsoft.WindowsMobile.Samples.Location;
using CodePlex.GpsTracker.GpxSupport;


namespace CodePlex.GpsTracker.Client
{
    public partial class GpsTracker : Form
    {
        private Gps _gps = new Gps();
        private Gpx _gpx = null;

        private double lastLatitude = 0.0;
        private double lastLongitude = 0.0;

        public GpsTracker()
        {
            InitializeComponent();
        }

        private void miExit_Click(object sender, EventArgs e)
        {
            if (_gps.Opened)
            {
                _gps.Close();
            }

            Close();
        }

        private void cmdStopTracking_Click(object sender, EventArgs e)
        {
            if (_gps.Opened)
            {
                _gps.Close();
            }

            mainTimer.Enabled = false;

            cmdStartNew.Enabled = true;
            cmdStartAppend.Enabled = true;
            cmdStopTracking.Enabled = false;
        }

        private void GpsTracker_Closing(object sender, CancelEventArgs e)
        {
            if (_gps.Opened)
            {
                _gps.Close();
            }
        }

        private void GpsTracker_Load(object sender, EventArgs e)
        {
            cboTimeUnit.Items.Add(new ComboBoxItem("Second", 1000));
            cboTimeUnit.Items.Add(new ComboBoxItem("Minute", 60000));
            cboTimeUnit.Items.Add(new ComboBoxItem("Hour", 3600000));

            cboTimeUnit.SelectedIndex = 0;

            tabMain.Height = Screen.PrimaryScreen.WorkingArea.Height - 30;
        }

        private void mainTimer_Tick(object sender, EventArgs e)
        {
            if (!_gps.Opened)
            {
                return;
            }

            GpsPosition position = _gps.GetPosition();

            if (!position.LatitudeValid || !position.LongitudeValid)
            {
                return;
            }

            try
            {
                double currentLatitude = position.RawLatitude;
                double currentLongitude = position.RawLongitude;

                if (currentLatitude != lastLatitude &&
                    currentLongitude != lastLongitude)
                {
                    Coordinate c = new Coordinate();
                    c.CoordinateTime = DateTime.Now;
                    c.Latitude = currentLatitude;
                    c.Longitude = currentLongitude;
                    c.Altitude = position.SeaLevelAltitudeValid ? position.SeaLevelAltitude : 0;
                    _gpx.AddRoutepoint(c);

                    lblLatitude.Text = currentLatitude.ToString();
                    lblLongitude.Text = currentLongitude.ToString();
                    
                    lastLatitude = currentLatitude;
                    lastLongitude = currentLongitude;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }           
        }

        private void timerTime_ValueChanged(object sender, EventArgs e)
        {
            mainTimer.Interval = (int)(timerTime.Value * (int)((ComboBoxItem)cboTimeUnit.SelectedItem).Value );
        }

        private void miSaveServer_Click(object sender, EventArgs e)
        {
        }

        private void cmdStartAppend_Click(object sender, EventArgs e)
        {
            StartGps();

            if (_gpx == null)
            {
                _gpx = new Gpx();
            }
        }

        private void cmdStartNew_Click(object sender, EventArgs e)
        {
            StartGps();

            _gpx = new Gpx();
        }

        private void StartGps()
        {
            if (!_gps.Opened)
            {
                _gps.Open();
            }

            mainTimer.Enabled = chbTrackRoute.Checked;
            cmdStartNew.Enabled = false;
            cmdStartAppend.Enabled = false;
            cmdStopTracking.Enabled = true;
        }

        
        private void cmdWaypointAdd_Click(object sender, EventArgs e)
        {
            GpsPosition position = _gps.GetPosition();

            if (!position.LatitudeValid || !position.LongitudeValid)
            {
                MessageBox.Show("We haven't valid position.", "GPS Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);                
                return;
            }

            Coordinate c = new Coordinate();
            c.CoordinateTime = DateTime.Now;
            c.Latitude = position.RawLatitude;
            c.Longitude = position.RawLongitude;
            c.Altitude = position.SeaLevelAltitudeValid ? position.SeaLevelAltitude : 0;

            _gpx.AddWaypoint(c);

            lblWaypointLatitude.Text = c.Latitude.ToString();
            lblWaypointLongitude.Text = c.Longitude.ToString();
        }

        private void miSaveLocal_Click(object sender, EventArgs e)
        {
            FrmSaveDataFile sdf = new FrmSaveDataFile();

            if (sdf.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            _gpx.Name = sdf.txtRouteName.Text;
            _gpx.Description = sdf.txtRouteDescription.Text;

            TextWriter w = new StreamWriter(sdf.txtFilePath.Text, false, Encoding.BigEndianUnicode);
            _gpx.GetGpx(w);
            w.Close();
        }
    }
}